package com.lab.account.web;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.common.service.ServiceException;
import com.lab.account.domain.Authority;
import com.lab.account.domain.Role;
import com.lab.account.service.AuthorityManager;
import com.lab.account.service.RoleManager;

@Controller
@RequestMapping("/account")
public class RoleController {

	@Autowired
	private RoleManager roleManager;
	@Autowired
	private AuthorityManager authorityManager;

	@RequestMapping(value = "/roles", method = RequestMethod.GET)
	public String list(HttpServletRequest request, HttpServletResponse response) {
		try {
			List<Role> list = roleManager.getAll();
			request.getSession().setAttribute("roleList", list);
		} catch (ServiceException e) {
			e.printStackTrace();
		}

		return "account/role-list";
	}

	@RequestMapping(value = "/roles/{roleId}", method = RequestMethod.GET)
	public String display(HttpServletRequest request,
			HttpServletResponse response, @PathVariable
			Long roleId) {
		HttpSession session = request.getSession();
		try {
			if (roleId == -1) {
				Collection<Authority> authList = authorityManager.getAll();
				session.setAttribute("availiableAuthList", authList);
				session.removeAttribute("role");
				session.removeAttribute("grantedAuthList");
			} else {
				Role role = roleManager.get(roleId);
				Collection<Authority> authList = authorityManager.getAll();
				Collection<Authority> grantedAuthList = role.getAuthorityList();
				Collection<Authority> availiableAuthList = CollectionUtils
						.subtract(authList, grantedAuthList);

				session.setAttribute("role", role);
				session.setAttribute("availiableAuthList", availiableAuthList);
				session.setAttribute("grantedAuthList", grantedAuthList);
			}
		} catch (ServiceException e) {
			e.printStackTrace();
		}

		return "account/role-display";
	}

	@RequestMapping(value = "/roles/{roleId}", method = RequestMethod.DELETE)
	public String delete(HttpServletRequest request,
			HttpServletResponse response, @PathVariable
			Long roleId) {
		try {
			roleManager.remove(roleId);
		} catch (ServiceException e) {
			e.printStackTrace();
		}

		return "redirect:/account/roles";
	}

	@RequestMapping(value = "/roles", method = RequestMethod.POST)
	public String save(HttpServletRequest request,
			HttpServletResponse response, Role role, String[] auth) {
		List<Authority> authList = new ArrayList<Authority>();
		for (String ro : auth) {
			Authority entity = new Authority();
			entity.setId(Long.parseLong(ro));

			authList.add(entity);
		}

		role.setAuthorityList(authList);

		try {
			roleManager.save(role);
		} catch (ServiceException e) {
			e.printStackTrace();
		}

		return "redirect:/account/roles";
	}

}
